/*
 * ProfileSettingsView.java
 *
 * Created on 12 Kasım 2008 Çarşamba, 12:50
 */
package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.controller.testdesign.ProfileSettingsSelectionController;
import org.testis.gui.mvc.utilities.AtofTableModel;
import java.awt.Frame;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author  nurhat
 */
public class ProfileSettingsSelectionView extends javax.swing.JDialog implements
        AbstractViewPanel {

    private static final long serialVersionUID = 19874328L;
    private TableRowSorter<TableModel> sorter;

    /** Creates new form ProfileSettingsView */
    public ProfileSettingsSelectionView(java.awt.Frame parent,
            boolean modal) {
        super(parent,
                modal);
        initComponents();
    }

    public ProfileSettingsSelectionView(Frame parent,
            boolean modal,
            AtofTableModel tableModel) {
        super(parent,
                modal);
        initComponents();
        sorter = new TableRowSorter<TableModel>(tableModel);
        profilesTable.setRowSorter(sorter);
        profilesTable.setModel(tableModel);
    }

    private void filterTable() {
        String text = txtFilter.getText();
        if (text.length() == 0) {
            sorter.setRowFilter(null);
        } else {
            sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        profilesTable = new javax.swing.JTable();
        btnExit = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtFilter = new javax.swing.JTextField();
        btnApplyToOtherProfiles = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Profiles");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(241, 240, 235));

        profilesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        profilesTable.setUpdateSelectionOnSort(false);
        jScrollPane1.setViewportView(profilesTable);

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        jLabel4.setText("Filter:");

        txtFilter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFilterActionPerformed(evt);
            }
        });

        btnApplyToOtherProfiles.setText("Apply To Selected Profiles");
        btnApplyToOtherProfiles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnApplyToOtherProfilesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFilter, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnExit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 301, Short.MAX_VALUE)
                        .addComponent(btnApplyToOtherProfiles)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExit)
                    .addComponent(btnApplyToOtherProfiles))
                .addGap(23, 23, 23))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
    this.setVisible(false);
    this.setEnabled(false);
    this.dispose();
}//GEN-LAST:event_btnExitActionPerformed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    WindowListener[] listeners = getWindowListeners();
    for (int i = 0; i < listeners.length; i++) {
        this.removeWindowListener(listeners[i]);
    }
    this.setVisible(false);
    this.setEnabled(false);
    TestDesignControllerList.newInstance().getProfileSettingsSelectionController().
            operationCloseWindow();
    this.dispose();
}//GEN-LAST:event_formWindowClosed

private void txtFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFilterActionPerformed
    filterTable();
}//GEN-LAST:event_txtFilterActionPerformed

private void btnApplyToOtherProfilesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApplyToOtherProfilesActionPerformed
    int[] selectedRows = profilesTable.getSelectedRows();
    if (selectedRows == null || selectedRows.length == 0) {
        return;
    }

    int[] profileIdList = new int[selectedRows.length];
    for (int i = 0; i < selectedRows.length; i++) {
        String strProfileId = (String) profilesTable.getValueAt(selectedRows[i], 0);
        profileIdList[i] = Integer.parseInt(strProfileId);
    }

    boolean result = TestDesignControllerList.newInstance().getProfileSettingsSelectionController().operationApplyToProfile(profileIdList);
    if (!result) {
        JOptionPane.showMessageDialog(
                this,
                "Changes can not applied for some profile",
                "Error Dialog",
                JOptionPane.ERROR_MESSAGE);
    }
    this.setVisible(false);
    this.setEnabled(false);
    this.dispose();
}//GEN-LAST:event_btnApplyToOtherProfilesActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnApplyToOtherProfiles;
    private javax.swing.JButton btnExit;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable profilesTable;
    private javax.swing.JTextField txtFilter;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                ProfileSettingsSelectionController.SHOW_VIEW)) {
            TestDesignControllerList.newInstance().getProfileSettingsSelectionController().
                    operationLoadProfiles();
            this.setVisible(true);


        } else if (evt.getPropertyName().equals(
                ProfileSettingsSelectionController.SHOW_ERROR)) {
            String val = (String) evt.getNewValue();
            JOptionPane.showMessageDialog(
                    this,
                    val,
                    "Error Dialog",
                    JOptionPane.ERROR_MESSAGE);
        } else if (evt.getPropertyName().equals(
                ProfileSettingsSelectionController.PARAMETER_MODEL_CHANGED)) {
            profilesTable.setModel((AtofTableModel) evt.getNewValue());
            profilesTable.getColumnModel().getColumn(0).setMaxWidth(20);
            profilesTable.repaint();
            sorter = new TableRowSorter<TableModel>((AtofTableModel) evt.getNewValue());
            profilesTable.setRowSorter(sorter);
        }
    }
}
